package o;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ResolveInfo;
import android.os.Binder;
import android.os.IBinder;
import android.os.IBinder.DeathRecipient;
import android.os.RemoteException;
import android.os.SystemClock;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import o.awi.e;

public class awh implements DeathRecipient {
    private Context ʻ;
    private ServiceConnection ʼ;
    private awh ʽ;
    private final HashSet<awe> ˊ;
    private final HashMap<awe, ArrayList<Integer>> ˋ;
    private final ArrayList<Integer> ˎ;
    private final a ˏ;
    private final Object ॱ;
    private awi ॱॱ;
    private awb ᐝ;

    final class a extends o.awk.a {
        final /* synthetic */ awh ˎ;

        private a(awh o_awh) {
            this.ˎ = o_awh;
        }

        public void ˏ(String str, int i, long j, long j2, String str2) {
            Log.i("PowerKitApi", "onPowerOverUsing moudle:" + str + " resource:" + i + " duration:" + j + " time:" + j2 + " extend:" + str2);
            synchronized (this.ˎ.ॱ) {
                if (this.ˎ.ˊ.isEmpty()) {
                    return;
                }
                Iterator it = this.ˎ.ˊ.iterator();
                while (it.hasNext()) {
                    awe o_awe = (awe) it.next();
                    ArrayList arrayList = (ArrayList) this.ˎ.ˋ.get(o_awe);
                    if (arrayList != null && arrayList.contains(Integer.valueOf(50))) {
                        o_awe.ˏ(str, i, j, j2, str2);
                    }
                }
            }
        }
    }

    public awh(Context context, awb o_awb) {
        this.ˊ = new HashSet();
        this.ˎ = new ArrayList();
        this.ˋ = new HashMap();
        this.ॱ = new Object();
        this.ˏ = new a();
        this.ʻ = null;
        this.ʽ = null;
        this.ᐝ = null;
        this.ॱॱ = null;
        this.ʼ = new ServiceConnection(this) {
            final /* synthetic */ awh ˋ;

            {
                this.ˋ = r1;
            }

            public void onServiceDisconnected(ComponentName componentName) {
                Log.v("PowerKitApi", "Powerkit service disconnected");
                this.ˋ.ᐝ.ˏ();
            }

            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                this.ˋ.ॱॱ = e.ˏ(iBinder);
                Log.v("PowerKitApi", "Powerkit service connected");
                this.ˋ.ᐝ.ॱ();
                try {
                    this.ˋ.ॱॱ.asBinder().linkToDeath(this.ˋ.ʽ, 0);
                } catch (Exception e) {
                    Log.w("PowerKitApi", "powerkit linkToDeath failed ! calling pid: " + Binder.getCallingPid());
                }
            }
        };
        this.ʽ = this;
        this.ᐝ = o_awb;
        this.ʻ = context;
        ॱ();
    }

    private boolean ॱ() {
        boolean bindService = this.ʻ.getApplicationContext().bindService(ˎ(this.ʻ, new Intent("com.huawei.android.powerkit.PowerKitService")), this.ʼ, 1);
        Log.v("PowerKitApi", "bind powerkit service, ret = " + bindService);
        return bindService;
    }

    private Intent ˎ(Context context, Intent intent) {
        String str;
        String str2;
        List queryIntentServices = context.getPackageManager().queryIntentServices(intent, 0);
        if (queryIntentServices == null || queryIntentServices.size() != 1) {
            Log.w("PowerKitApi", "not only one match for intent: " + intent);
            str = "com.huawei.powergenie";
            str2 = "com.huawei.android.powerkit.PowerKitService";
        } else {
            ResolveInfo resolveInfo = (ResolveInfo) queryIntentServices.get(0);
            str = resolveInfo.serviceInfo.packageName;
            str2 = resolveInfo.serviceInfo.name;
        }
        ComponentName componentName = new ComponentName(str, str2);
        Log.i("PowerKitApi", "match for intent,  packageName:" + str + " className:" + str2);
        Intent intent2 = new Intent(intent);
        intent2.setComponent(componentName);
        return intent2;
    }

    public boolean ˎ(Context context, boolean z, String str, int i, long j, String str2) throws RemoteException {
        if (this.ॱॱ != null) {
            return this.ॱॱ.ˏ(context.getPackageName(), z, str, i, j, str2);
        }
        throw new RemoteException("PowerKit server is not found");
    }

    public void binderDied() {
        Log.e("PowerKitApi", "powerkit process binder was died and connecting ...");
        this.ॱॱ = null;
        int i = 5;
        while (i > 0) {
            i--;
            SystemClock.sleep((long) (new Random().nextInt(2001) + 1000));
            if (ॱ()) {
                return;
            }
        }
    }
}
